/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#include "probe.h"
#include <string.h>

/* ******************************************************************************************* */

int build_port_name(char *port_name,char *port_location,int port_number){
/* build the name of the port corresponding to the requested number */
/* return EXIT_SUCCESS or EXIT_FAILURE */
	int number;		/* multipurpose integer */
	
	/* checking that the required port number is within the accepted range */
	if(port_number>SERIAL_PORTS_MAX) {
		error_log(_("build_port_name: asking for a port number > SERIAL_PORTS_MAX"));
		return EXIT_FAILURE;
	}
	/* building the serial port file name */
	number=snprintf(port_name,(size_t)(FILE_NAME_LENGTH*sizeof(char)),"%s%d",port_location,port_number);
	if(number <= -1 || number >= (int)(FILE_NAME_LENGTH*sizeof(char))) {
		error_log(_("build_port_name: port descriptor string building failure\n"));
		return EXIT_FAILURE;
	}
	
	return EXIT_SUCCESS;
}

int start_probe(struct probe_descriptor *probe,struct configuration_struct *settings) {
/* open and initialize a port decsribed by its structure "probe_descriptor" */
/* return EXIT_SUCCESS or EXIT_FAILURE */
	char port_name[FILE_NAME_LENGTH];		/* the place where we will read the probe values */
	
	/* initializing and run the probes */
	printf(_("\tInitializing probe #%d\n"),probe->index);
	/* probe has not yet recorded events */
	probe->value=0;
	/* probe location information */
	probe->location[0]='\0';
	strncat(probe->location,settings->generic_location,(size_t)FILE_NAME_LENGTH);
	if(build_port_name(port_name,probe->location,probe->number) == EXIT_FAILURE) {
		return EXIT_FAILURE;
	}
	
	/* initialization of the port */
	#ifdef DEBUG
		printf("\tPort \"%s\" will be opened\n",port_name);
	#endif	
	if((probe->file_descriptor=open_port(port_name)) == EXIT_FAILURE) {
		error_log(_("start_probe: port can not be opened"));
		return EXIT_FAILURE;
	}
	if(settings->is_serial==EXIT_SUCCESS) {
	/* probes are real serial ports, so they need some initializations */
		if((set_port(probe->file_descriptor)) == EXIT_FAILURE) {
			error_log(_("start_probe: serial port can not be set up"));
			return EXIT_FAILURE;
		}
		if((dtr_up(probe->file_descriptor)) == EXIT_FAILURE) {
			error_log(_("start_probe: DTR line of port can not be raised"));
			return EXIT_FAILURE;
		}
	}
		
	return EXIT_SUCCESS;
}

int stop_probe(struct probe_descriptor *probe,struct configuration_struct *settings) {
/* close port and shutdown the device */
/* return EXIT_SUCCESS or EXIT_FAILURE */

	if(settings->is_serial==EXIT_SUCCESS) {
	/* probes are real serial ports, powering detectors OFF */
		if((dtr_down(probe->file_descriptor)) == EXIT_FAILURE) {
			error_log(_("stop_probe: DTR line of port can not be lowered"));
		}
	}
	/* end of probe acquisition, we close the port */
	if((close_port(probe->file_descriptor)) == EXIT_FAILURE) {
		error_log(_("stop_probe: port can not be closed"));
		return EXIT_FAILURE;
	}
	
	return EXIT_SUCCESS;
}
